<?php

namespace app\admin\controller;

use app\service\Tool;

class System extends Base
{
    public function index()
    {
        $this->logs('[管理员]系统配置视图');
        $data = config('system');
        return view('index', ['data' => $data]);
    }
    public function update()
    {
        $this->logs('[管理员]更新系统配置');
        $param = input();
        $param['site'] = !array_key_exists('site',$param) ? 0 : 1;
        $param['login'] = !array_key_exists('login',$param) ? 0 : 1;
        $param['register'] = !array_key_exists('register',$param) ? 0 : 1;
        $param['carouselSw'] = !array_key_exists('carouselSw',$param) ? 0 : 1;
        $param['reg_examine'] = !array_key_exists('reg_examine',$param) ? 0 : 1;
        if (config('system')['base']['logo'] != $param['logo']) {
            $path = app()->getRootPath() . 'public' . config('system')['base']['logo'];
            if (file_exists($path)) unlink($path);
        }
        // if (config('system')['carousel']['carousel1'] != $param['carousel1']) {
        //     $path = app()->getRootPath() . 'public' . config('system')['carousel']['carousel1'];
        //     if (file_exists($path)) unlink($path);
        // }
        // if (config('system')['carousel']['carousel2'] != $param['carousel2']) {
        //     $path = app()->getRootPath() . 'public' . config('system')['carousel']['carousel2'];
        //     if (file_exists($path)) unlink($path);
        // }
        // if (config('system')['carousel']['carousel3'] != $param['carousel3']) {
        //     $path = app()->getRootPath() . 'public' . config('system')['carousel']['carousel3'];
        //     if (file_exists($path)) unlink($path);
        // }
        $path = app()->getRootPath() . 'config/system.php';
        foreach ($param as $k => $v) {
            $text = file_get_contents($path);
            preg_match("/(.*)$k(.*)/", $text, $match);
            if (isset($match[0])) {
                $changeVal = str_replace($match[0], "'{$k}' => '{$v}',", $text);
                file_put_contents($path, trim($changeVal));
            }
        }
        return view('tip/index', ['msg' => '修改成功']);
    }
    public function uploadImg()
    {
        $images = request()->file('img_path');
        return Tool::uploadImg($images, 'carousel');
    }
}
